package com.itheima.servcie;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.bean.CodeBean;
import com.itheima.mongoservice.FriendServiceApi;
import com.itheima.mongoservice.UserLikeServiceApi;
import com.itheima.mongoservice.VisitorsServiceApi;
import com.itheima.pojo.mongo.Friend;
import com.itheima.vo.FriendPageList;
import com.itheima.vo.RecommendFriend;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class UserLikeService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @DubboReference
    private UserLikeServiceApi userLikeServiceApi;
    @DubboReference
    private VisitorsServiceApi visitorsServiceApi;
    @DubboReference
    private FriendServiceApi friendServiceApi;
    /**
     * 粉丝 - 喜欢
     * @param id
     * @return
     */
    public ResponseEntity addUserLike(Long id) {
        //获取uid
        Long uid = Convert.toLong(stringRedisTemplate.opsForValue().get(CodeBean.ID));
        //调用api获取喜欢
        userLikeServiceApi.addUserLike(uid,id);

        return ResponseEntity.ok("查询喜欢成功");
    }

    /**
     * 喜欢 - 取消
     * @param id
     * @return
     */
    public ResponseEntity deleteUserLike(Long id) {
        //获取uid
        Long uid = Convert.toLong(stringRedisTemplate.opsForValue().get(CodeBean.ID));
        //操作friend表中查询该id是否存在
        List<Friend> friends = friendServiceApi.findFriends(uid);
        //判断该id是否为friend表中的字段
        if (!ObjectUtil.isNull(friends)){
            //删除friend表直接之间的好友关系,然后将该成员添加至userlike表中
            friendServiceApi.deleteFriend(uid,id);
        }

        //删除喜欢用户表中该用户
        userLikeServiceApi.deleteUserLike(uid,id);
        //返回
        return ResponseEntity.ok("取消喜欢成功");
    }

    /**
     * 互相喜欢，喜欢，粉丝 - 统计
     * @return
     */
    public ResponseEntity getTotalCount() {
        //获取uid
        Long uid = Convert.toLong(stringRedisTemplate.opsForValue().get(CodeBean.ID));
        Map map=userLikeServiceApi.getTotalCount(uid);
        //返回结果
        return ResponseEntity.ok(map);
    }


    //互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
    public ResponseEntity pageList(Integer type, Integer page, Integer pagesize, String nickname) {
        //获取id
        Long uid = Convert.toLong(stringRedisTemplate.opsForValue().get(CodeBean.ID));
        //判断id
        List<FriendPageList> result = new ArrayList<>();
        switch (type){
            //相互喜欢
            case 1:
                result = userLikeServiceApi.eachPageList(uid, page, pagesize,nickname);
                break;
            //喜欢分页
            case 2:
                result = userLikeServiceApi.likePageList(uid, page, pagesize,nickname);
                break;
            //粉丝分页
            case 3:
                result = userLikeServiceApi.fanPageList(uid, page, pagesize,nickname);
                break;
            //谁看过我分页
            case 4:
                result=visitorsServiceApi.visitorsPageList(uid, page, pagesize,nickname);
        }
        //将数据及进行返回
        RecommendFriend<FriendPageList> pageList = new RecommendFriend<>(page,pagesize,result);
        return ResponseEntity.ok(pageList);
    }
}
